<?php

class Categorias_Model extends Model {

    public function listaCategorias()
    {
        try {
            $query = "SELECT catCod,
                             catCodPai, 
                             (select catNom FROM nws_produto_categoria G WHERE G.catCod = N.catCodPai) as subCat,
                             catNom,
                             catSta 
                      FROM nws_produto_categoria N
                      ORDER BY catCodPai ASC";
            $rs = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query);
            return $rs;
        } catch (Doctrine_Exception $e) {
            print $e->getMessage();
        }   
    }   
    
    public function insertCategoria()
    {
        $emailNovo = new NwsProdutoCategoria;
        $emailNovo->setcatCodPai($_POST['catCodPai']);
        $emailNovo->setcatNom($_POST['catNom']);
        $emailNovo->setcatImg($_POST['catImg']);
        $emailNovo->setcatSta($_POST['catSta']);
        $emailNovo->insertCategoria();
        header('location:'. BASE_URL_ADM . 'categorias');
    }
    
    public function excluirCategoria($catCod)
    {
        $categoria = Doctrine::getTable('NwsProdutoCategoria')->find($catCod);
        if($categoria) {
            $categoria->delete();
            header('location:'. BASE_URL_ADM . 'categorias/');
        } else {
            echo "errado";
        }
    }
    
    
    
    public function teste()
    {
        try {
            $query = "SELECT catCod,
                             catCodPai, 
                             (select catNom FROM nws_produto_categoria G WHERE G.catCod = N.catCodPai) as subCat,
                             catNom,
                             catSta 
                      FROM nws_produto_categoria N
                      ORDER BY catCodPai ASC";
            $rs = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query);
            return $rs;
        } catch (Doctrine_Exception $e) {
            print $e->getMessage();
        }   
    }   
    
}

?>
